<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>{{ $route.query.type == "add" ? '新增成员' : "编辑成员信息" }}</span>
      </div>
    </template>

    <div class="add-form" v-loading="loading">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
        status-icon
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请填写员工姓名"></el-input>
        </el-form-item>

        <el-form-item label="账号" prop="departCount">
          <el-input v-model="ruleForm.departCount" placeholder="请填写员工账号"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="zhuchiren">
          <el-input v-model="ruleForm.zhuchiren" placeholder="请填写员工邮箱"></el-input>
        </el-form-item>

        <el-form-item label="员工类型" prop="fayanren">
          <el-input v-model="ruleForm.fayanren" placeholder="请填写员工类型"></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="buttonLoading">
            {{ $route.query.type == "add" ? '新增' : "编辑" }}
          </el-button>
          <el-button @click="resetForm(ruleFormRef)" :loading="buttonLoading">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
export default { name: 'backendSystemDepartAdd' }
</script>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router'
import { UploadFilled } from '@element-plus/icons-vue'
import { http } from '@/utils/http/index';

/* 路由 */
const route = useRouter()

onMounted(() => {
  if(route.currentRoute.value.query.id){
    getMemberDetail()
  }
})

/* 详情数据加载动画 */
const loading = ref(false)

/* 查询详情 */
const getMemberDetail = () => {
  return
  loading.value = true
  http.$GET("/PreachM/GetPreachTeamDetailById", { Id: route.currentRoute.value.query.id }).then(res => {
    loading.value = false
    if (res.isError) return
    if (!res.result) return
    
    /* 赋值 */
    Object.keys(ruleForm.value).forEach((key)=>{
      if(res.result[key] != null){
        ruleForm.value[key] = res.result[key];
      }
    })

  }).catch(err => {
    loading.value = false
    ElMessage.warning("查询失败")
  })
}

/* 新增、编辑表单 */
const ruleFormRef = ref(null)

/* 表单数据 */
const ruleForm = ref({
  id: "",
  name: "",
  departCount: "",
  zhuchiren: "",
  fayanren: "",
})

/* 校验规则 */
const rules = reactive({
  name: [
    { required: true, message: '请填写员工姓名', trigger: 'blur' }
  ],
  departCount: [
    { required: true, message: '请填写员工账号', trigger: 'blur' }
  ],
})

/* 提交新增或编辑时，按钮禁用 */
const buttonLoading = ref(false)

/* 提交表单 */
const submitForm = (formName) => {
  ruleFormRef.value.validate((valid, fields) => {
    if(valid) {
      buttonLoading.value = true

      /* 新增时不传id */
      if(route.currentRoute.value.query.type == 'add'){
        delete ruleForm.value.id
      }

      console.log(ruleForm.value)
      return

      /* 参数处理 */
      const formData = new FormData();
      for(let k in ruleForm.value){
        formData.append(k, ruleForm.value[k]);
      }

      for (var value of formData.values()) {
        console.log(value);
      }

      http.$POST("/PreachM/AddOrUpdatePreachTeamInfo", formData).then(res => {
        buttonLoading.value = false
        if (res.IsError) {
          ElMessage.warning('操作失败');
        }else{
          ElMessage.success('操作成功');
          route.push({
            path: "/backend/publicize-city"
          })
        }
      }).catch(err => {
        buttonLoading.value = false
        ElMessage.warning('操作失败');
      })
    }else{
      ElMessage.warning('请先完成必填项');
    }
  })
}

/* 重置表单 */
const resetForm = (formName) => {
  ruleFormRef.value.resetFields()
}
</script>

<style lang='scss' scoped>
@import "@/assets/styles/backendElement.scss";
</style>